import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/refresh_listview.dart';
import 'package:gzmall_app/common/toast_util.dart';
import 'package:gzmall_app/goods/goods_details_page.dart';
import 'package:gzmall_app/model/product.dart';
import 'product_item_widget.dart';

class ActivesListWidget extends StatefulWidget {
  int id;

  ActivesListWidget(this.id);

  @override
  State<StatefulWidget> createState() => ActivesListWidgetState(this.id);
}

class ActivesListWidgetState extends State<ActivesListWidget> {
  List<Product> _list = [];
  int id;

  double _itemWidth;

  int _curPage = 1;
  RefreshListView _refreshListView;

  ActivesListWidgetState(this.id);

  void init() {
    var size = MediaQuery.of(context).size;
    _itemWidth = (size.width - 15) / 2;
  }

  @override
  void initState() {
    super.initState();
    _getList(id, 1);
  }

  @override
  Widget build(BuildContext context) {
    init();
    return _buildGirdList();
  }

  Widget _buildGirdList() {
    List<Widget> _ws = _list
        .map((item) => ProductItemWidget(item, _itemWidth, () {
              if (item.id != null) {
                Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                  return new GoodsDetailsPage(productId: item.id);
                }));
              }
            }))
        .toList();
    _refreshListView = RefreshListView(
      enablePullUp: true,
      enablePullDown: true,
      onRefresh: _onRefresh,
      child: GridView.count(
        shrinkWrap: true,
        padding: const EdgeInsets.only(left: 5.0, right: 5.0, top: 5.0),
        crossAxisSpacing: 5.0,
        mainAxisSpacing: 5.0,
        controller: new ScrollController(keepScrollOffset: false),
        crossAxisCount: 2,
        childAspectRatio: _itemWidth / (_itemWidth + _itemWidth / 3),
        children: _ws,
      ),
    );
    return Container(color: Colors.white, child: _refreshListView);
  }

  void _onRefresh(bool up) {
    if (up) {
      _getList(id, 1);
    } else {
      _getList(id, _curPage + 1);
    }
  }

  _getList(int shopCategoryId, int page) {
    bool isGetNew = page == 1;
    Map<String, String> params = {
      "page": page.toString(),
      "shop_category_id": shopCategoryId.toString(),
    };
    NetUtil.get(
        context,
        Api.spus,
        (data, message) {
          if (!mounted) return;
          _curPage = page;
          var _dataList = (data as List)
              ?.map((e) => e == null ? null : Product.fromJson(e))
              ?.toList();
          setState(() {
            if (isGetNew) {
              _list = _dataList;
            } else {
              _list.addAll(_dataList);
            }
          });
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew,
                hasMore: _dataList != null && _dataList.length >= Api.pageSize);
          }
        },
        params: params,
        errorCallBack: (e) {
          if (_refreshListView != null) {
            _refreshListView.refreshCompleted(isGetNew);
          }
        });
  }
}
